Class Button extends Box
private static var pressing=false;
//类型(1无效果/2两态按钮/3热区按钮)
public var type=1;
public var toggleMode=false;
public var delayMode=true;

var label;
var img;
var arrowSign;
var toggled=false;
var src;
var text;
var toggleColor;
var toggleBgColor;
var baseColor;
var baseBgColor;
var arrow=false;
function Button(text,src){
	this.subEnabled=false;
	if(this.__class==Button&&(text||src)){
		this.text=text;
		this.src=src;
		if(text&&!src){
			this.setTextButton();
		}
		else if(src){
			this.setImgButton()
		}
	}
}
function setTextButton(){
	this.type=3;
	this.bg=["#fff","#ccc",5];
	this.label=this.addChild(new Label(this.text));
	this.label.__x="c";
	this.label.__y="c";
	this.label.w=this.label.getClientW();
	this.label.h=this.label.getClientH();
	this.setSize(160,60);
}
function setImgButton(){
	this.img=this.addChild(new Img(this.src));
	this.img.name="base";
	if(IO.resHash[this.src+"@"]){
		this.type=2;
		this.img=this.addChild(new Img(this.src+"@"));
		this.img.name="base";
	}

	if(this.text){
		this.label=this.addChild(new Label(this.text));
	}
	this.setSize(this.img.w,this.img.h);
}
public function resetSelf(){
	if(this.type==2&&this.children.length>1){
		this.children[0].setVisible(!this.toggled);
		this.children[1].setVisible(this.toggled);
	}
	else if(this.type==3){
		if(!is(Array,this.bg)) this.bg=[this.bg];
		if(!this.baseBgColor){
			this.baseBgColor=this.bg[0]||"#fff";
		}
		if(!this.toggleBgColor){
			var c=G.toColor(this.bg[0]||"#fff");
			var c2="#";
			for(var i=1;i<6;i+=2){
				var n=int(int("0x"+c.slice(i,i+2))*0.92);
				c2+=(n<16?"0":"")+n.toString(16);
			}
			this.toggleBgColor=c2;
		}
		this.bg[0]=!this.toggled?this.baseBgColor:this.toggleBgColor;
		this.setBg(this.bg);
	}
	if(this.toggleColor){
		if(!this.label) this.label=this.getChild("label");
		if(!this.baseColor) this.baseColor=this.label.color;
		if(this.label){
			this.label.setColor(!this.toggled?this.baseColor:this.toggleColor);
		}
	}
	if(this.arrowSign){
		var h=this.getClientH()-(!this.padding?0:2*int(this.padding[0]));
		this.arrowSign.setPos("r-32",(h-this.arrowSign.h)/2);
	}

}
public function click(){
	G.target=this;
	this.dispatchEvent(G.newEvent(Evt.BUTTON_CLICK));
}
public function setSize(w,h){
	Box.proto.setSize.call(this,w,h);
	var box=this.children[0];
	if(box&&box.name=="base") box.setSize(w,h);
	box=this.children[1];
	if(box&&box.name=="base") box.setSize(w,h);
	if(this.label) this.label.setPos(this.label.__x,this.label.__y);
	this.changeSelf();
}
public function setSrc(v){
	this.src=v;
	var resArr=IO.resHash[v];
	if(resArr){
		this.w=resArr[2];
		this.h=resArr[3];
	}
	if(this.img) this.img.setSrc(v);
	this.changeSelf();
}
public function setText(v){
	this.text=v;
	if(this.label){
		this.label.setText(v);
		this.label.w=this.label.getClientW();
	}
	this.changeSelf();
}
public function resetBg(v){
	this.bg=v;
	this.baseBgColor=null;
	this.changeSelf();
}
public function setToggled(v){
	this.toggled=v;
	this.changeSelf();
}
public function setArrow(v){
	this.arrow=v;
	if(!v&&this.arrowSign){
		this.arrowSign.remove();
		this.arrowSign=null;
	}
	if(v&&!this.arrowSign){
		var c=is(String,this.arrow)?this.arrow:"#bbb";
		this.arrowSign=this.addChild(new Box({absolute:true,w:12,h:20,bg:[,"2"+c,"shape",[0,0],[12,10],[0,20]]}));
		this.changeSelf();
	}
}
function onPress(){
	if(Me.pressing) return;
	Me.pressing=true;
	if(this.toggleMode) return;
	if(!this.delayMode||!this.inScroll()){
		this.setToggled(true);
	}
	else{
		callLater([this,this.onPress2],150);
	}
}
function onPress2(){
	if(G.isPressMove){
		Me.pressing=false;
		return;
	}
	this.setToggled(true);
}
function onRelease(){
	if(!Me.pressing) return;
	Me.pressing=false;
	if(this.toggleMode) return;
	var clicked=false;
	if(this.inScroll()){
		if(!G.isPressMove) clicked=true;
	}
	else{
		if(this.hitTest(G.x,G.y)) clicked=true;
	}
	if(!this.delayMode){
		this.setToggled(false);
		if(clicked) this.click();
	}
	else{
		callLater([this,this.onRelease2,clicked],200);
	}
}
function onRelease2(clicked){
	this.setToggled(false);
	if(this.delayMode&&clicked) this.click();
}
function onClick(){
	if(!this.toggleMode) return;
	this.setToggled(!this.toggled);
	this.click();
}